<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>09_猜数字练习</title>
</head>
<body>
    <h1>已生成一个 1～100之间的随机数</h1>
    <input type="text" id="input1">
    <button onclick="f()">点我验证</button>
    <h2>小提示：<span></span></h2>
    <script>
        var num = Math.floor(Math.random()*100)+1;
        console.log(num);
        let count = 0;
        function f(){
            const v1 = parseInt(input1.value);
            if (!/^\d+$/.test(v1)){
                alert("请输入数字")
                return;
            }
            var h2 = document.querySelector("span");
            count++;
            if(v1>num){
                h2.innerHTML = "太大啦";
            }else if(v1<num){
                h2.innerHTML = "太小啦";
            }else{
                h2.innerHTML = "恭喜你用了"+count+"次就猜对啦!";
            }
        }
    </script>
</body>
</html>